package com.jzt.jk.open.api.manage.domain.sys.service;

import com.jzt.jk.open.api.manage.application.assembler.CommonAssembler;
import com.jzt.jk.open.api.manage.domain.sys.bo.ManagerLoginReqBo;
import com.jzt.jk.open.api.manage.domain.sys.bo.ManagerLoginResBo;
import com.jzt.jk.open.api.manage.infrastructure.common.config.GlobalConfig;
import com.jzt.jk.open.api.manage.infrastructure.common.config.TokenConfig;
import com.jzt.jk.open.api.manage.infrastructure.common.model.UserInfo;
import com.jzt.jk.open.api.manage.infrastructure.repository.EnumConstant;
import com.jzt.jk.open.api.manage.infrastructure.repository.dao.MenuMapper;
import com.jzt.jk.open.api.manage.infrastructure.repository.dao.UserMapper;
import com.jzt.jk.open.api.manage.infrastructure.repository.po.Menu;
import com.jzt.jk.open.api.manage.infrastructure.repository.po.User;
import com.jzt.jk.open.api.manage.infrastructure.util.AesUtils;
import com.jzt.jk.open.api.manage.infrastructure.util.CookieUtils;
import com.jzt.jk.open.api.manage.infrastructure.util.JwtTokenUtils;
import com.jzt.jk.open.api.manage.infrastructure.util.TupleTow;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Objects;

/**
 * 作者：lizw <br/>
 * 创建时间：2021/01/21 11:32 <br/>
 */
@Transactional(readOnly = true)
@Service
@Slf4j
public class LoginService {
    @Autowired
    private GlobalConfig globalConfig;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private MenuMapper menuMapper;

    public ManagerLoginResBo managerLogin(HttpServletResponse response, ManagerLoginReqBo reqBo) {
        ManagerLoginResBo resBo = new ManagerLoginResBo();
        resBo.setSuccess(false);
        User user = userMapper.getByLoginNameOrTelephone(reqBo.getLoginName());
        if (user == null) {
            resBo.setMessage("用户名或密码错误");
            return resBo;
        }
        // 密码加密
        String password;
        try {
            password = AesUtils.encode(globalConfig.getPasswordAesKey(), globalConfig.getPasswordAesIv(), reqBo.getPassword());
        } catch (Exception e) {
            log.warn("密码加密失败", e);
            resBo.setMessage("用户名或密码错误");
            return resBo;
        }
        // 密码比对
        if (!Objects.equals(password, user.getPassword())) {
            resBo.setMessage("用户名或密码错误");
            return resBo;
        }
        // 用户未启用
        if (!Objects.equals(user.getEnabled(), EnumConstant.Enabled_1)) {
            resBo.setMessage("账号当未启用");
            return resBo;
        }
        // 登录成功 - 生成JWT-Token
        TokenConfig tokenConfig = globalConfig.getTokenConfig();
        UserInfo userInfo = CommonAssembler.toBo(user, UserInfo.class);
        TupleTow<String, Claims> tupleTow = JwtTokenUtils.createJwtToken(tokenConfig, userInfo);
        CookieUtils.setCookie(response, "/", tokenConfig.getJwtTokenName(), tupleTow.getValue1(), (int) tokenConfig.getTokenValidity().getSeconds());
        // 返回
        resBo.setSuccess(true);
        resBo.setJwtToken(tupleTow.getValue1());
        resBo.setUserInfo(userInfo);
        return resBo;
    }

    public List<Menu> getMenus(String uid, Integer scope) {
        return menuMapper.findByUidAndScope(uid, scope);
    }
}
